package com.android.SchoolManagerSystem.action;

import java.net.HttpURLConnection;
import java.util.HashMap;
import java.util.List;

import org.xml.sax.Attributes;

import android.sax.EndElementListener;
import android.sax.EndTextElementListener;
import android.sax.RootElement;
import android.sax.StartElementListener;
import android.util.Xml;

import com.android.SchoolManagerSystem.CommonInfo;
import com.android.SchoolManagerSystem.SchoolManagerSystem;
import com.android.SchoolManagerSystem.data.Userinfo;
import com.android.SchoolManagerSystem.net.HttpRequester;
import com.android.SchoolManagerSystem.net.HttpResponser;

public class UserLogin {
	private Userinfo curinfo = null;
	boolean berrorcode = true;

	public UserLogin(Userinfo curuser) {
		curinfo = curuser;
	}

	/**
	 * login api for user
	 * 
	 * @author oracle_lei
	 * @date 2011-7-14
	 */
	public boolean login() {
		//if(SchoolManagerSystem.bDebug)
			//return true;
		HashMap<String, String> propertys = new HashMap<String, String>();
		propertys.put("Set-Cookie", "ONE_VER=1_0;sid=10006");
		propertys.put("H-Product", "client");

		String strrequetstXMl = generatexmlstring();
		HttpRequester loginrequest = new HttpRequester();
		HttpResponser loginresponse = loginrequest.sendPost(
				CommonInfo.strloginuri, propertys, strrequetstXMl);
		if (loginresponse == null
				|| loginresponse.getCode() != HttpURLConnection.HTTP_OK)
			return false;
		else {
			if (decodelogresponce(loginresponse))
				return true;
			else
				return false;
		}
	}

	private String generatexmlstring() {
		String strlogreq = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
				+ "<userlogins apiversion=\"1.0\"><userid>" + curinfo.struserid
				+ "</userid><password>" + curinfo.getuserpsw()
				+ "</password><usertype>" + curinfo.usertype
				+ "</usertype></userlogins>";
		return strlogreq;
	}

	private boolean decodelogresponce(HttpResponser repsonse) {
		RootElement root = new RootElement("userloginr");

		root.setStartElementListener(new StartElementListener() {
			@Override
			public void start(Attributes attributes) {
			}
		});
		root.setEndElementListener(new EndElementListener() {
			@Override
			public void end() {
			}
		});

		root.getChild("name").setEndTextElementListener(
				new EndTextElementListener() {

					@Override
					public void end(String body) {
						// TODO Auto-generated method stub
						if (body != null) {
							berrorcode = false;
							curinfo.strusername = body;
						}
					}
				});
		root.getChild("class").setEndTextElementListener(
				new EndTextElementListener() {

					@Override
					public void end(String body) {
						// TODO Auto-generated method stub
						if (body != null) {
							curinfo.strClass = body;
						}
					}
				});
		root.getChild("username").setEndTextElementListener(
				new EndTextElementListener() {

					@Override
					public void end(String body) {
						// TODO Auto-generated method stub
						if (body != null) {
							
							if (curinfo.usertype == "1")
								curinfo.struserid = body;
							else
							{
								berrorcode = false;
								curinfo.strusername = body;
							}

						}
					}
				});
		root.getChild("sex").setEndTextElementListener(
				new EndTextElementListener() {

					@Override
					public void end(String body) {
						// TODO Auto-generated method stub
						if (body != null) {
							curinfo.strSex = body;
						}
					}
				});

		root.getChild("birthDate").setEndTextElementListener(
				new EndTextElementListener() {

					@Override
					public void end(String body) {
						// TODO Auto-generated method stub
						if (body != null) {
							curinfo.strBirthday = body;
						}
					}
				});

		root.getChild("nation").setEndTextElementListener(
				new EndTextElementListener() {

					@Override
					public void end(String body) {
						// TODO Auto-generated method stub
						if (body != null) {
							curinfo.strNation = body;
						}
					}
				});
		root.getChild("birthPlace").setEndTextElementListener(
				new EndTextElementListener() {

					@Override
					public void end(String body) {
						// TODO Auto-generated method stub
						if (body != null) {
							curinfo.strBirthPlace = body;
						}
					}
				});
		root.getChild("politics").setEndTextElementListener(
				new EndTextElementListener() {

					@Override
					public void end(String body) {
						// TODO Auto-generated method stub
						if (body != null) {
							curinfo.strpolitics = body;
						}
					}
				});
		root.getChild("mobile").setEndTextElementListener(
				new EndTextElementListener() {

					@Override
					public void end(String body) {
						// TODO Auto-generated method stub
						if (body != null) {
							curinfo.strMobileNo = body;
						}
					}
				});
		root.getChild("bedroom").setEndTextElementListener(
				new EndTextElementListener() {

					@Override
					public void end(String body) {
						// TODO Auto-generated method stub
						if (body != null) {
							curinfo.strBedroom = body;
						}
					}
				});
		root.getChild("homePlace").setEndTextElementListener(
				new EndTextElementListener() {

					@Override
					public void end(String body) {
						// TODO Auto-generated method stub
						if (body != null) {
							curinfo.strHomeTown = body;
						}
					}
				});

		root.getChild("errorcode").setEndTextElementListener(
				new EndTextElementListener() {
					@Override
					public void end(String body) {
						int ires = body.compareTo("0");
						if (ires == 0)
							berrorcode = false;
						else
							berrorcode = true;
					}
				});

		try {
			Xml.parse(repsonse.getContent(), root.getContentHandler());
		} catch (Exception e1) {
			e1.printStackTrace();
			return false;
		}
		if (berrorcode)
			return false;
		else
			return true;
	}

	private void foreach() {
		// TODO Auto-generated method stub

	}
}
